package zky.medicalInstitute.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * File tools
 *
 * @author Abner_阿锋
 * @version 1.0
 * @date 2021/12/8
 */
public class FileUtils {
    /**

     * 获取文件路径

     * filePath=/storage/emulated/0/Android/data/com.example.wifiapplication/files/20120101_100216_log.txt

     * */

    public static  String getFilesPath(Context context) {

// 20120101_100216_log.txt 用时间格式创建文件名

        Calendar c = Calendar.getInstance();

       // SimpleDateFormat fdate = new SimpleDateFormat("yyyyMMdd_HHmmss");

       // String fname = fdate.format(c.getTime()) + "心电数据.txt";
        String fname="滤波心电数据.txt";
        String filePath ;

        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {

//外部存储可用

            filePath = context.getExternalFilesDir(null).getPath() + "/" + fname;

        } else {

//外部存储不可用

            filePath = context.getFilesDir().getPath() + "/" + fname;

        }

        return filePath;

    }

    public static  void write(String filePath,String content){
        try {

            FileOutputStream outputStream = new FileOutputStream(filePath, true);// true 为 追加写入，false 为删除写入(把之前的数据清除掉，再写入)

            outputStream.write(content.getBytes());

            //outputStream.write("99999999".getBytes());

            outputStream.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }
    }
    //写入原始路径
    public static  String getOrginateFilesPath(Context context,String id) {

// 20120101_100216_log.txt 用时间格式创建文件名

        Calendar c = Calendar.getInstance();

        // SimpleDateFormat fdate = new SimpleDateFormat("yyyyMMdd_HHmmss");

        // String fname = fdate.format(c.getTime()) + "心电数据.txt";

        String fname=id+"原始心电数据.txt";
        String filePath ;

        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {

//外部存储可用

            filePath = context.getExternalFilesDir(null).getPath() + "/" + fname;

        } else {

//外部存储不可用

            filePath = context.getFilesDir().getPath() + "/" + fname;

        }

        return filePath;

    }
//写入16进制原始信号
    public static  void orginatewrite(String filePath,String content){
        try {

            FileOutputStream outputStream = new FileOutputStream(filePath, true);// true 为 追加写入，false 为删除写入(把之前的数据清除掉，再写入)

            outputStream.write(content.getBytes());

            //outputStream.write("99999999".getBytes());

            outputStream.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }
    }


}

